package com.webservice.impl;


import com.dao.FileDao;
import com.domain.AssetsFile;
import com.domain.DownloadPassword;
import com.webservice.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.sql.Struct;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private FileDao fileDao;

    @Override
    public AssetsFile findFile(String fileName){
        AssetsFile assetsFile = fileDao.findFileName(fileName);
        return assetsFile;
    }

    @Override
    public DownloadPassword findDownloadValue(String key) {
        DownloadPassword downloadPassword = new DownloadPassword();
        if (!StringUtils.isEmpty(key)){
            downloadPassword = fileDao.findDownloadValue(key);
            if (downloadPassword != null){
                return downloadPassword;
            }
        }
        return downloadPassword;
    }

    @Override
    public int addDownloadPassword(DownloadPassword downloadPassword) {
        if (downloadPassword != null){
            if (!StringUtils.isEmpty(downloadPassword.getDownloadKey()) &&
                    !StringUtils.isEmpty(downloadPassword.getDownloadValue())){
                fileDao.addDownloadPassword(downloadPassword);
                return 0;
            }
            else {
                return -1;
            }
        }
        else {
            return -2;
        }
    }

    @Override
    public int updateDownloadPassword(String key, String value) {
        if (!StringUtils.isEmpty(key) &&
                !StringUtils.isEmpty(value)){
            fileDao.updateDownloadPassword(key, value);
            return 0;
        }
        else {
            return -1;
        }
    }

    @Override
    public int delDownloadPassword(String key) {
        if (!StringUtils.isEmpty(key)){
            DownloadPassword downloadPassword = fileDao.findDownloadValue(key);
            if (downloadPassword!=null){
                fileDao.delDownloadPassword(key);
                return 0;
            }
            else{
                return -1;
            }
        }
        return -2;
    }

}
